using System;
using System.Collections.Generic;
using System.Data.Linq;
using RomanJendrusz.Amazon.ETL.Infrastructure;
using System.Linq;

namespace RomanJendrusz.Amazon.ETL.Domain
{
    public class BrowseNode
    {
        private int nodeId;
        private string name;
        private int? parentId;
        private EntityRef<BrowseNode> parentNode;
        private DateTime updated;
        private EntitySet<BrowseNodesBook> nodesBooks;

        public  BrowseNode()
        {
            parentNode = new EntityRef<BrowseNode>();   
            nodesBooks =  new EntitySet<BrowseNodesBook>();
        }

        public BrowseNode(int nodeId, string name)
        {
            this.nodeId = nodeId;
            this.name = name;
            parentNode =  new EntityRef<BrowseNode>();
            nodesBooks = new EntitySet<BrowseNodesBook>();
        }

        public int BrowseNodeId
        {
            get{ return nodeId;}
            set{ nodeId = value;}
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public BrowseNode ParentNode
        {
            set
            {
                AssertThat.IsNotNull(value, "Parent");
                parentNode.Entity = value;
            }
            get
            {
                return parentNode.Entity;
            }
        }

        public bool IsCategoryRoot{get; set;}

        public void AddBook(Book book)
        {
            var nodesBook = new BrowseNodesBook(this, book);
            nodesBooks.Add(nodesBook);
        }

        public IEnumerable<Book> Books
        {
            get
            {
                foreach(var nodesBook in nodesBooks)
                {
                    yield return nodesBook.Book;
                }
            }
        }

        public DateTime Updated
        {
            get { return updated; }
        }

        public override string ToString()
        {
            return name;
        }
    }
}